﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using PaynetApi.Utility;
using PaynetApi.Requests;
namespace PaynetApi
{
    public class PaynetGateway
    {
        private enum tr_type
        {
            SALE,
            CREDIT,
            PREAUTH,
            FORCE,
            VOID
        }
        private enum cc_method
        {
            Swiped,
            DataEntry
        }
        public const string LIVE_URL = "https://www.securepay.com/secure1/index.asp";


        public string MerchId { get; set;}
		public string TransKey { get; set;}
		public bool TestMode { get; set;}
        public PaynetGateway(string merchId, string transKey)
		{
            MerchId = merchId;
            TransKey = transKey;
		}

        public Response SwipePreAuth(string Amount, string MagData)
        {
            PostSubmitter post = new PostSubmitter();
            post.PostItems.Add("Amount", Amount);
            post.PostItems.Add("Email", "NA");  //required by the api but not very useful for retail.
            post.PostItems.Add("Merch_ID", MerchId);
            post.PostItems.Add("TransKey", TransKey);
            post.PostItems.Add("Tr_Type", tr_type.PREAUTH.ToString());
            post.PostItems.Add("CC_Method", cc_method.Swiped.ToString());
            post.PostItems.Add("SwipeData", MagData);
            post.Type = PostSubmitter.PostTypeEnum.Post;
            string result = post.Post(LIVE_URL);
            Response res = new Response(result);
            return res;
        }

        public Response SwipeForce(string Amount, string MagData, string ApprovalNumber)
        {
            PostSubmitter post = new PostSubmitter();
            post.PostItems.Add("Amount", Amount);
            post.PostItems.Add("Email", "NA");  //required by the api but not very useful for retail.
            post.PostItems.Add("Merch_ID", MerchId);
            post.PostItems.Add("TransKey", TransKey);
            post.PostItems.Add("ApprovNumber", ApprovalNumber);
            post.PostItems.Add("Tr_Type", tr_type.FORCE.ToString());
            post.PostItems.Add("CC_Method", cc_method.Swiped.ToString());
            post.PostItems.Add("SwipeData", MagData);
            post.Type = PostSubmitter.PostTypeEnum.Post;
            string result = post.Post(LIVE_URL);
            Response res = new Response(result);
            return res;
        }

        public Response ManualPreAuth(string Amount, string CCNumber, string ExpiryYear, string ExpiryMonth, string Ccy)
        {
            PostSubmitter post = new PostSubmitter();
            post.PostItems.Add("Amount", Amount);
            post.PostItems.Add("Email", "NA");  //required by the api but not very useful for retail.
            post.PostItems.Add("Merch_ID", MerchId);
            post.PostItems.Add("TransKey", TransKey);
            post.PostItems.Add("Tr_Type", tr_type.PREAUTH.ToString());
            post.PostItems.Add("CC_Method", cc_method.DataEntry.ToString());
            post.PostItems.Add("CC_Number", CCNumber);
            post.PostItems.Add("Year", ExpiryYear);
            post.PostItems.Add("Month", ExpiryMonth);
            post.PostItems.Add("AVSREQ", "0");

            post.Type = PostSubmitter.PostTypeEnum.Post;
            string result = post.Post(LIVE_URL);
            Response res = new Response(result);
            return res;
        }

        public Response ManualForce(string Amount, string CCNumber, string ExpiryYear, string ExpiryMonth, string Ccy, string ApprovalNumber)
        {
            PostSubmitter post = new PostSubmitter();
            post.PostItems.Add("Amount", Amount);
            post.PostItems.Add("Email", "aloupos@gmail.com");
            post.PostItems.Add("Merch_ID", MerchId);
            post.PostItems.Add("TransKey", TransKey);
            post.PostItems.Add("ApprovNumber", ApprovalNumber);
            post.PostItems.Add("Tr_Type", tr_type.FORCE.ToString());
            post.PostItems.Add("CC_Method", cc_method.DataEntry.ToString());
            post.PostItems.Add("CC_Number", CCNumber);
            post.PostItems.Add("Year", ExpiryYear);
            post.PostItems.Add("Month", ExpiryMonth);
            post.PostItems.Add("AVSREQ", "0");
            post.Type = PostSubmitter.PostTypeEnum.Post;
            string result = post.Post(LIVE_URL);
            Response res = new Response(result);
            return res;
        }

    }

}
